package org.onosproject.ngsdn.tutorial.common;

import org.onlab.packet.Ip4Address;
import org.onlab.packet.IpAddress;
import org.onosproject.net.Host;
import org.onosproject.net.Link;
import org.onosproject.net.Path;

import java.util.Objects;


public class HostToHosLinks {
    private Host srcHost;
    private Host dstHost;
    private Path path;


    public HostToHosLinks(Host srcHost, Host dstHost){
        this.srcHost = srcHost;
        this.dstHost = dstHost;
    }

    public HostToHosLinks(Host srcHost, Host dstHost, Path path){
        this.srcHost = srcHost;
        this.dstHost = dstHost;
        this.path = path;
    }

    public boolean isPathLinkChange(Link cLink){
        return path.links().contains(cLink);
    }

    public Path getPath(){
        return this.path;
    }
    public void setPath(Path path){ this.path = path;}
    public void rmPath(){ this.path = null;}
    public Host getSrcHost(){return this.srcHost; }
    public Host getDstHost(){return this.dstHost; }

    public Ip4Address getSrcIpv4(){
        return this.srcHost.ipAddresses().stream()
                .filter(IpAddress::isIp4)
                .map(IpAddress::getIp4Address)
                .findFirst()
                .get();
    }

    public Ip4Address getDstIpv4(){
        return this.dstHost.ipAddresses().stream()
                .filter(IpAddress::isIp4)
                .map(IpAddress::getIp4Address)
                .findFirst()
                .get();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        HostToHosLinks that = (HostToHosLinks) o;
        return Objects.equals(srcHost, that.srcHost) && Objects.equals(dstHost, that.dstHost);
    }

    @Override
    public int hashCode() {
        return Objects.hash(srcHost, dstHost);
    }
}
